import time
from selenium.webdriver.common.by import By
from selenium import webdriver

class  Online_loanOperation:

###################################      贷款操作       ###################################

    def Online_loan_before(self,driver,username,password):
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        time.sleep(1)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        time.sleep(1)
        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)


    def Online_loan_apply(self,driver,loan_amount,loan_term):
        # 点击金融工具
        driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[3]/a").click()
        time.sleep(1)
        # 点击安全网贷
        driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[3]/ul/li[2]/a").click()
        time.sleep(1)
        # 输入借款金额
        driver.find_element(By.XPATH,
                            "/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/input").send_keys(
            loan_amount)
        time.sleep(1)
        # 输入借款期限
        driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/input").send_keys(
            loan_term)
        time.sleep(1)
        # 点击申请
        driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[4]/button").click()
        time.sleep(1)
        # 点击确定
        driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]").click()
        time.sleep(1)
        try:
            driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button").click()
            time.sleep(1)
        except:
            time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)

###################################        审核        ###################################

    def Loan_review(self):

        driver = webdriver.Chrome()
        #窗口最大化
        driver.maximize_window()
        time.sleep(2)
        #跳转网址
        driver.get("http://localhost:90")
        time.sleep(1)
        # 输入管理员用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys("admin")
        time.sleep(1)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        time.sleep(1)
        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)
        # 点击网贷银行
        driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[5]/a").click()
        time.sleep(1)
        # 点击网贷审核
        driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[5]/ul/li[1]/a").click()
        time.sleep(1)
        # 获取末页的位置
        ul = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[3]/div[2]/nav/ul")
        lis = ul.find_elements(By.XPATH, "li")
        num = len(lis)
        time.sleep(1)
        # 点击末页
        driver.find_element(By.XPATH,
                            f"/html/body/div/div/main/div/div/div/div/div[3]/div[2]/nav/ul//li[{num}]/a").click()
        time.sleep(1)
        # 获取当前页面贷款记录数量
        tbody = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody")
        trs = tbody.find_elements(By.XPATH, "tr")
        num1 = len(trs)
        time.sleep(1)
        # 点击最后一条贷款信息审核通过
        driver.find_element(By.XPATH,
                            f"//html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[{num1}]/td[7]/button[1]").click()
        time.sleep(1)
        # 点击确定
        driver.find_element(By.XPATH,"/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]").click()
        time.sleep(1)

        # 退出浏览器
        driver.quit()

###################################    获取借款记录数量     ###################################
    def loannum(self,driver):
        # 点击我的借贷
        driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[4]/ul/li[2]/a").click()
        time.sleep(1)
        try:
            # 获取末页的位置
            ul = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div[2]/div[3]/nav/ul")
            lis = ul.find_elements(By.XPATH, "li")
            num = len(lis)
            time.sleep(1)
            # 点击末页
            driver.find_element(By.XPATH,
                                f"/html/body/div/div/main/div/div/div/div/div[2]/div[2]/div[3]/nav/ul/li[{num}]/a").click()
            time.sleep(1)
            # 获取当前页面贷款记录数量
            tbody = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div[1]/table/tbody")
            trs = tbody.find_elements(By.XPATH, "tr")
            num1 = len(trs)
        except:
            num1 = "1"
        return num1
###################################    获取银行卡余额     ###################################
    def money(self,driver):
        # 点击银行卡管理
        driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[4]/ul/li[3]/a").click()
        time.sleep(1)
        # 获取银行卡账户余额
        num = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[2]/div/div/div[2]/h4[2]").text[5:-1]
        num = float(num)
        time.sleep(1)
        return num

###################################    获取实际结果     ###################################
    def getapplyResult(self,num1,num2):
        if num1 != num2:
            result = "成功"
        else:
            result = "失败"
        return result

    def getreviewResult(self,num1,num2):
        if num1 == num2:
            result = "成功"
        else:
            result = "失败"
        return result